package cn.cnsy123.bookwebsiteserver.controller.dbinfo;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.cnsy123.bookwebsiteserver.entity.SysDataBaseInfo;
import cn.cnsy123.bookwebsiteserver.param.DbConfigInsertParam;
import cn.cnsy123.bookwebsiteserver.param.DbConfigUpdateParam;
import cn.cnsy123.bookwebsiteserver.service.dbinfo.DbInfoService;
import cn.cnsy123.bookwebsiteserver.vo.layui.CommonResult;
import cn.cnsy123.bookwebsiteserver.vo.layui.TableResult;

/**
 * Description:NOSQL数据源配置中心<br/>
 * CreateTime:2019-04-12<br/>
 * Creater: yangpengfei360@126.com <br/>
 * @author yangpengfei
 */
@Controller
@RequestMapping(value = "dbInfo")
public class DBController {
    
    private static final Logger logger = LoggerFactory.getLogger(DBController.class);
    
    @Autowired
    private DbInfoService dbInfoService;
    
    @GetMapping(value = "page")
    public ModelAndView dbInfoPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("dbInfoManager");
        modelAndView.setStatus(HttpStatus.OK);
        return modelAndView;
    }
    
    @GetMapping(value = "list")
    @ResponseBody
    public TableResult<SysDataBaseInfo> findAllDbInfo(
            @RequestParam(value = "page",required = true,defaultValue = "1") Integer page,
            @RequestParam(value = "limit",required = true,defaultValue = "10") Integer size){
        TableResult<SysDataBaseInfo> tableResult = null;
        try {
            tableResult = dbInfoService.findAllDbInfo(page.intValue(),size.intValue());
        } catch (Exception e) {
            logger.error("NOSQL数据源信息获取失败",e);
            //记录错误信息到数据库
            //返回默认错误处理信息
            tableResult = new TableResult<SysDataBaseInfo>(0,"信息获取失败，请通知系统管理员",0L,null);
        }
        return tableResult;
    }
    
    @DeleteMapping(value = "delete/{dbInfoId}")
    @ResponseBody
    public CommonResult<Object> deleteDbConfigInfo(@PathVariable(value = "dbInfoId") Long dbInfoId){
        CommonResult<Object> commonResult = null;
        try {
            commonResult = dbInfoService.deleteDbConfigInfo(dbInfoId);
        }catch(Exception e) {
            logger.error("删除数据源信息[dbInfoId="+dbInfoId+"]错误",e);
            commonResult = new CommonResult<>(1, "删除失败");
        }
        return commonResult;
    }
    
    @PostMapping(value = "update")
    @ResponseBody
    public CommonResult<Object> updateDbConfigInfo(@Valid DbConfigUpdateParam param){
        CommonResult<Object> commonResult = null;
        try {
            commonResult = dbInfoService.updateDbConfigInfo(param);
        }catch(Exception e) {
            logger.error("修改数据源信息[dbInfoId="+param.getDbInfoId()+"]错误",e);
            commonResult = new CommonResult<>(1, "更新失败");
        }
        return commonResult;
    }
    
    @PutMapping(value = "insert")
    @ResponseBody
    public CommonResult<Object> insertDbConfigInfo(@Valid DbConfigInsertParam param){
        CommonResult<Object> commonResult = null;
        try {
            commonResult = dbInfoService.insertDbConfigInfo(param);
        }catch(Exception e) {
            logger.error("新增数据源信息[dbName="+param.getDbName()+"]错误",e);
            commonResult = new CommonResult<>(1, "保存失败");
        }
        return commonResult;
    }

}
